/*!
 * @file       apm32f10x_usart.h
 *
 * @brief      This file contains all the functions prototypes for the USART firmware library
 *
 * @details   
 *
 * @version    V1.0.0
 *   
 * @date       2019-8-7
 *
 */

#ifndef __APM32F10x_USART_H
#define __APM32F10x_USART_H

#ifdef __cplusplus
 extern "C" {
#endif

#include "apm32f10x.h"

/**
 * @brief   USART Word Length define
 */
typedef enum
{
   USART_WORD_LEN_8B = 0,
   USART_WORD_LEN_9B = BIT12
}USART_WORD_LEN_T;

/**
 * @brief   USART Stop bits   define
 */
typedef enum
{
   USART_STOP_BIT_1    = 0,
   USART_STOP_BIT_0_5    = BIT12,
   USART_STOP_BIT_2    = BIT13,
   USART_STOP_BIT_1_5    = BIT12 | BIT13
}USART_STOP_BITS_T;

/**
 * @brief   USART Parity define
 */
typedef enum
{
   USART_PARITY_NONE    = 0,
   USART_PARITY_EVEN    = BIT10,
   USART_PARITY_ODD    = BIT10 | BIT9
}USART_PARITY_T;

/**
 * @brief   USART mode define
 */
typedef enum
{
   USART_MODE_RX       = BIT2,
   USART_MODE_TX       = BIT3,
   USART_MODE_TX_RX    = BIT2 | BIT3
}USART_MODE_T;

/**
 * @brief   USART hardware flow control define
 */
typedef enum
{
   USART_FLOW_CTRL_NONE    = 0,
   USART_FLOW_CTRL_RTS    = BIT8,
   USART_FLOW_CTRL_CTS    = BIT9,
   USART_FLOW_CTRL_RTS_CTS = BIT8 | BIT9
}USART_HARDWARE_FLOW_CTRL_T;

/**
 * @brief   USART CK Pin enable
 */
typedef enum
{
   USART_CK_PIN_DISABLE,
   USART_CK_PIN_ENABLE
}USART_CK_PIN_T;

/**
 * @brief   USART Clock polarity define
 */
typedef enum
{
   USART_CLKPOL_LOW,
   USART_CLKPOL_HIGH
}USART_CLKPOL_T;

/**
 * @brief   USART Clock phase define
 */
typedef enum
{
   USART_CLKPHA_1EDGE,
   USART_CLKPHA_2EDGE
}USART_CLKPHA_T;

/**
 * @brief   USART Last bit clock pulse enable
 */
typedef enum
{
   USART_LBC_DISABLE,
   USART_LBC_ENABLE,
}USART_LBC_T;

/**
 * @brief   USART Interrupt Source
 */
typedef enum
{
   USART_INT_PE    = 0x0010100,
   USART_INT_TXE    = 0x7010080,
   USART_INT_TC    = 0x6010040,
   USART_INT_RXNE    = 0x5010020,
   USART_INT_IDLE    = 0x4010010,
   USART_INT_LBD    = 0x8020040,
   USART_INT_CTS    = 0x9040400,
   USART_INT_ERR    = 0x3040001,
   USART_INT_ORE    = 0x3040001,
   USART_INT_NE    = 0x2040001,
   USART_INT_FE    = 0x1040001
}USART_INT_T;


/**
 * @brief   USART DMA enable
 */
typedef enum
{
   USART_DMA_TX    = BIT7,
   USART_DMA_RX    = BIT6,
   USART_DMA_TX_RX = BIT6 | BIT7
}USART_DMA_T;

/**
 * @brief   USART Wakeup method
 */
typedef enum
{
   USART_WAKEUP_IDLE_LINE,
   USART_WAKEUP_ADDRESS_MARK
}USART_WAKEUP_T;

/**
 * @brief   USART LIN break detection length
 */
typedef enum
{
   USART_LBDL_10B,
   USART_LBDL_11B
}USART_LBDL_T;

/**
 * @brief   USART IrDA low-power
 */
typedef enum
{
   USART_IRDALP_NORMAL,
   USART_IRDALP_LOWPOWER
}USART_IRDALP_T;

/**
 * @brief   USART flag define
 */
typedef enum
{
   USART_FLAG_CTS       = 0x0200,
   USART_FLAG_LBD       = 0x0100,
   USART_FLAG_TXBE    = 0x0080,
   USART_FLAG_TXC       = 0x0040,
   USART_FLAG_RXBNE    = 0x0020,
   USART_FLAG_IDLE    = 0x0010,
   USART_FLAG_OVRE    = 0x0008,
   USART_FLAG_NE       = 0x0004,
   USART_FLAG_FE       = 0x0002,
   USART_FLAG_PE       = 0x0001
}USART_FLAG_T;

/**
 * @brief   USART Config struct definition
 */
typedef struct
{
   uint32_t baudRate;
   USART_WORD_LEN_T wordLength;
   USART_STOP_BITS_T stopBits;
   USART_PARITY_T parity;
   USART_MODE_T mode;
   USART_HARDWARE_FLOW_CTRL_T hardwareFlowCtrl;
}USART_ConfigStruct_T;


void USART_Reset(USART_T* usart);
void USART_Config(USART_T* uart, USART_ConfigStruct_T *configStruct);
void USART_ConfigCKPin(USART_T* uart, USART_CK_PIN_T ckPin);
void USART_CofigClkPolarity(USART_T* uart, USART_CLKPOL_T polarity);
void USART_ConfigClkPhase(USART_T* uart, USART_CLKPHA_T phase);
void USART_ConfigLastBitClock(USART_T* uart, USART_LBC_T lbc);
void USART_StructInit(USART_ConfigStruct_T* configStruct);
void USART_Enable(USART_T* usart);
void USART_Disable(USART_T* usart);
void USART_EnableTX(USART_T * usart);
void USART_DisableTX(USART_T * usart);
void USART_EnableRX(USART_T * usart);
void USART_DisableRX(USART_T * usart);
void USART_EnableInterrupt(USART_T *usart, USART_INT_T intSrc);
void USART_DisableInterrupt(USART_T *usart, USART_INT_T intSrc);
void USART_EnableDMA(USART_T *usart, USART_DMA_T mdaReq);
void USART_DisableDMA(USART_T *usart, USART_DMA_T mdaReq);
void USART_SetAddress(USART_T* usart, uint8_t address);
void USART_ConfigWakeUp(USART_T* usart, USART_WAKEUP_T wakeupMethod);
void USART_EnableMuteMode(USART_T* usart);
void USART_DisableMuteMode(USART_T* usart);
void USART_ConfigLINBreakDetectLength(USART_T* usart, USART_LBDL_T LBDL);
void USART_EnableLIN(USART_T* usart);
void USART_DisableLIN(USART_T* usart);
void USART_TxData(USART_T* usart, uint16_t data);
uint16_t USART_RxData(USART_T* usart);
void USART_TxBreak(USART_T* usart);
void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime);
void USART_ConfigDivNumber(USART_T* usart, uint8_t div);
void USART_EnableSmartCard(USART_T* usart);
void USART_DisableSmartCard(USART_T* usart);
void USART_EnableSmartCardNACK(USART_T* usart);
void USART_DisableSmartCardNACK(USART_T* usart);
void USART_EnableHalfDuplex(USART_T* usart);
void USART_DisableHalfDuplex(USART_T* usart);
void USART_ConfigIrDA(USART_T* usart, USART_IRDALP_T IrDAMode);
void USART_EnableIrDA(USART_T* usart);
void USART_DisableIrDA(USART_T* usart);
uint8_t USART_ReadFlag(USART_T* usart, USART_FLAG_T flag);
void USART_ClearFlag(USART_T* usart, USART_FLAG_T flag);
uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_T intSrc);
void USART_ClearIntFlag(USART_T* usart, USART_INT_T intSrc);

#ifdef __cplusplus
}
#endif

#endif /* __APM32F10x_USART_H */

